<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
    $carousel = \App\Models\Upload::where('caption', 'carousel')->get();
    $about = \App\Models\SinglePage::where('key', 'index-about')->first();
    $contact = \App\Models\SinglePage::where('key', 'index-contact')->first();
    $news = \App\Models\News::all();
    $hiring = \App\Models\SinglePage::where('key', 'index-hiring')->first();
    $notice = \App\Models\SinglePage::where('key', 'index-notice')->first();
    $staffStyle = \App\Models\StaffStyle::all();
    $products = \App\Models\Product::all()->groupBy(function ($i) {
        return $i->brand;
    });
     $title = implode(' - ', array_merge(
        [\App\Models\Config::where('name','company')->first()->value]
    ));

    return view('index')->with(compact('title', 'carousel', 'about', 'contact', 'news', 'hiring', 'notice',
        'staffStyle', 'products'));
});

Route::get('/page/{column}/{name}', ['as' => 'page', function ($colId, $name) {
    $currentColumn  = \App\Models\Column::find($colId);
    $title = implode(' - ', array_merge(
        $currentColumn->title()
        ,[\App\Models\Config::where('name','company')->first()->value]
    ));
    $associateColumns = $currentColumn->associateColumns();
    $breadCrumb = $currentColumn->breadCrumb();

    $page = \App\Models\SinglePage::where('key', $name)->first();

    if (!is_null($page)) {
        $bodyData = $page->content;
        return view('app.pages.default')->with(compact('title','bodyData', 'associateColumns', 'breadCrumb', 'currentColumn'));
    } else {
        abort(404);
    }
}]);

Route::get('/news/{classify}/{colId}',[ 'as' => 'news', function ( $classify, $colId) {

    $currentColumn  = \App\Models\Column::find($colId);
    $title = implode(' - ', array_merge(
        $currentColumn->title()
        ,[\App\Models\Config::where('name','company')->first()->value]
    ));
    $breadCrumb = $currentColumn->breadCrumb();
    $associateColumns = $currentColumn->associateColumns();
    $bodyData   = \App\Models\News::where('classify', $classify)->get();

    return view('app.pages.news')->with(compact('title', 'breadCrumb', 'associateColumns', 'currentColumn', 'bodyData'));
}]);

Route::get('/news_page/{id}', ['as' => 'news_page', function($id){
    $newsPage = \App\Models\News::find($id);
    $currentColumn  = \App\Models\Column::where('url', 'like', '%'.$newsPage->classify )->first();
    $title = implode(' - ', array_merge(
        $currentColumn->title()
        ,[\App\Models\Config::where('name','company')->first()->value]
    ));
    $breadCrumb = $currentColumn->breadCrumb();
    $associateColumns = $currentColumn->associateColumns();
    $bodyData   = $newsPage->content;

    return view('app.pages.default')->with(compact('title', 'breadCrumb', 'associateColumns', 'currentColumn', 'bodyData'));
}]);

Route::get('/contact_us/{id}', ['as' => 'contact_us', function($id){
    $currentColumn  = \App\Models\Column::find($id);
    $title = implode(' - ', array_merge(
        $currentColumn->title()
        ,[\App\Models\Config::where('name','company')->first()->value]
    ));
    $breadCrumb = $currentColumn->breadCrumb();
    $associateColumns = $currentColumn->associateColumns();
    $bodyData   = '';

    return view('app.pages.contact')->with(compact('title', 'breadCrumb', 'associateColumns', 'currentColumn', 'bodyData'));
}]);

Route::post('/contact_us', ['as' => 'contact_us.store', 'uses' => 'MsgController@submit']);

Route::get('/staff_presence/{id}', ['as' => 'staff_presence', function($id){
    $currentColumn  = \App\Models\Column::find($id);
    $title = implode(' - ', array_merge(
        $currentColumn->title()
        ,[\App\Models\Config::where('name','company')->first()->value]
    ));
    $breadCrumb = $currentColumn->breadCrumb();
    $associateColumns = $currentColumn->associateColumns();
    $bodyData   = \App\Models\StaffStyle::all()->groupBy('classify');

    return view('app.pages.staff_presence')->with(compact('title', 'breadCrumb', 'associateColumns', 'currentColumn', 'bodyData'));
}]);

Route::get('/staff_presence_show/{colId}/{imageGroupName}', ['as' => 'staff_presence_show', function($id, $imagGroupName){
    $currentColumn  = \App\Models\Column::find($id);
    $title = implode(' - ', array_merge(
        $currentColumn->title()
        ,[\App\Models\Config::where('name','company')->first()->value]
    ));
    $breadCrumb = $currentColumn->breadCrumb();
    $associateColumns = $currentColumn->associateColumns();
    $bodyData   = \App\Models\StaffStyle::where('classify', $imagGroupName)->get();

    return view('app.pages.staff_presence_show')->with(compact('title', 'breadCrumb', 'associateColumns', 'currentColumn', 'bodyData'));
}]);



/* ================== Homepage + Admin Routes ================== */

require __DIR__ . '/admin_routes.php';
